package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConst;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service
public class SetmealServicImpl implements SetmealServic {

	@Autowired
	JedisPool jedisPool;

	@Autowired
	SetmealDao setmealDao;

	@Override
	@Transactional
	public void add(Setmeal setmeal, Integer[] checkgroupIds) {

		//注意需要主键回显
		setmealDao.add(setmeal);

		if(setmeal.getId()!=null && checkgroupIds !=null && checkgroupIds.length>0){

			for (Integer checkgroupId:checkgroupIds
				 ) {

				setmealDao.addRelation(setmeal.getId(),checkgroupId);

			}

			jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES,setmeal.getImg());


		}



	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {

		PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
		Page<Setmeal> page =  setmealDao.findPage(queryPageBean.getQueryString());
		return new PageResult(page.getTotal(),page);
	}

	@Override
	public List<Setmeal> getSetmeal() {



		return setmealDao.getSetmeal();
	}

	@Override
	public Setmeal findDetailsById(Integer id) {

		return setmealDao.findDetailsById(id);


	}

	@Override
	public Setmeal findById(Integer id) {

		return setmealDao.findById(id);
	}
}
